package com.xuxin.baby.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.lxj.xpopup.XPopup;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.UserLikeAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UserLikeBean;
import com.xuxin.baby.contract.CollectContract;
import com.xuxin.baby.contract.UserLikeContract;
import com.xuxin.baby.popup.ConfirmPopup;
import com.xuxin.baby.presenter.CollectPresenter;
import com.xuxin.baby.presenter.UserLikePresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.27 0027
 * 我的收藏夹
 */
public class UserLikeActivity extends BaseActivity implements UserLikeContract.View, CollectContract.View {
    private final UserLikeContract.Presenter presenter = new UserLikePresenter(this);
    private final CollectContract.Presenter presenter2 = new CollectPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private UserLikeAdapter myAdapter;
    private String memberId;//用户id
    private String tenantId;//租户id
    private String token;//授权token
    private int pageNum = 0;
    private String title;//标题

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        title_name.setText("收藏夹");
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                pageNum = 0;
                myAdapter.setList(null);
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onLoadMore");
                handler.sendEmptyMessage(0);
            }
        });
        RecycleUtils.init(smart_recycle);
        myAdapter = new UserLikeAdapter();
        smart_recycle.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString(BaseConstant.videoId, myAdapter.getData().get(position).getCourseId());
                ActivityUtils.startActivity(bundle, VideoActivity.class);
            }
        });
        myAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                new XPopup.Builder(context)
                        .dismissOnTouchOutside(true)
                        .dismissOnBackPressed(true)
                        .asCustom(new ConfirmPopup(context, "确定要取消收藏吗？", new ConfirmPopup.OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                try {
                                    Message message = Message.obtain();
                                    message.what = 1;
                                    message.obj = myAdapter.getData().get(position).getId();
                                    popup.setTitle("取消收藏中...").show();
                                    handler.sendMessage(message);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    dismiss();
                                }
                            }
                        })).show();
                return false;
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        memberId = aCache.getAsString(BaseConstant.memberId);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        dismiss();
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getCollect(tenantId, token, memberId, pageNum);
            } else if (msg.what == 1) {
                presenter2.deleteCollect(tenantId, token, memberId, (String) msg.obj);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(UserLikeBean userLikeBean) {
        myAdapter.addData(userLikeBean.getResult().getItems());
        if (userLikeBean.getResult().getTotalCount() > myAdapter.getData().size()) {
            pageNum++;
            smart_refresh.setEnableLoadMore(true);
        } else {
            smart_refresh.setEnableLoadMore(false);
        }
    }

    @Override
    public void onCollectSuccess(BaseBean baseBean) {

    }

    @Override
    public void onDeleteCollectSuccess(BaseBean baseBean) {
        pageNum = 0;
        myAdapter.setList(null);
        handler.sendEmptyMessage(0);
    }
}